home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / jade / src / amiga_clipboard.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  4KB  |  188 lines

  1. /* amiga_clipboard.c --  code for interfacing with the Amigas iffparse.library
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with Jade; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22.  
  23. #include <clib/iffparse_protos.h>
  24. #include <libraries/iffparse.h>
  25.  
  26. _PR void  clip_kill(void);
  27. _PR bool  write_clip(long, u_char *, long);
  28. _PR VALUE read_clip(long);
  29.  
  30. /*
  31.  * IFF code, adapted from the newiff/other/clipftxt.c example on the 2.0
  32.  * native developer set.
  33.  */
  34.  
  35. #define ID_FTXT  MAKE_ID('F','T','X','T')
  36. #define ID_CHRS  MAKE_ID('C','H','R','S')
  37.  
  38. static const VALUE IFFErrorMsgs[] =
  39. {
  40.     MKSTR("End of file."),
  41.     MKSTR("End of context."),
  42.     MKSTR("No lexical scope."),
  43.     MKSTR("No memory."),
  44.     MKSTR("Stream read error."),
  45.     MKSTR("Stream write error."),
  46.     MKSTR("Stream seek error."),
  47.     MKSTR("File is corrupt."),
  48.     MKSTR("IFF syntax error."),
  49.     MKSTR("Not an IFF file."),
  50.     MKSTR("Required call-back hook missing."),
  51.     MKSTR("Return to client.")
  52. };
  53.  
  54. void *IFFParseBase;
  55.  
  56. void
  57. clip_kill(void)
  58. {
  59.     if(IFFParseBase)
  60.     {
  61.     CloseLibrary(IFFParseBase);
  62.     IFFParseBase = NULL;
  63.     }
  64. }
  65.  
  66. /*
  67.  */
  68. bool
  69. write_clip(long unit, u_char *str, long strLen)
  70. {
  71.     if(IFFParseBase || (IFFParseBase = OpenLibrary("iffparse.library", 36)))
  72.     {
  73.     struct IFFHandle *iff;
  74.     long error = 0;
  75.     if(iff = AllocIFF())
  76.     {
  77.         if(iff->iff_Stream = (ULONG)OpenClipboard(unit))
  78.         {
  79.         InitIFFasClip(iff);
  80.         if(!(error = OpenIFF(iff, IFFF_WRITE)))
  81.         {
  82.             if(!(error = PushChunk(iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN)))
  83.             {
  84.             if(!(error = PushChunk(iff, 0, ID_CHRS, IFFSIZE_UNKNOWN)))
  85.             {
  86.                 if(WriteChunkBytes(iff, str, strLen) != strLen)
  87.                 error = IFFERR_WRITE;
  88.                 if(!error)
  89.                 error = PopChunk(iff);
  90.                 else
  91.                 PopChunk(iff);
  92.             }
  93.             if(!error)
  94.                 error = PopChunk(iff);
  95.             else
  96.                 PopChunk(iff);
  97.             }
  98.             CloseIFF(iff);
  99.         }
  100.         CloseClipboard((struct ClipboardHandle *)iff->iff_Stream);
  101.         }
  102.         else
  103.         error = IFFERR_NOMEM;    /* ?? */
  104.         FreeIFF(iff);
  105.     }
  106.     else
  107.         error = IFFERR_NOMEM;
  108.  
  109.     CloseLibrary(IFFParseBase);
  110.     IFFParseBase = NULL;
  111.  
  112.     if(error)
  113.     {
  114.         cmd_signal(sym_error, list_2(MKSTR("iffparse"), IFFErrorMsgs[-error - 1]));
  115.         return(FALSE);
  116.     }
  117.     return(TRUE);
  118.     }
  119.     cmd_signal(sym_error, LIST_1(MKSTR("Can't open iffparse.library")));
  120.     return(FALSE);
  121. }
  122.  
  123. /*
  124.  * note:
  125.  *  Currently this only reads the first CHRS chunk that it finds.
  126.  */
  127. VALUE
  128. read_clip(long unit)
  129. {
  130.     VALUE text = NULL;
  131.     if(IFFParseBase || (IFFParseBase = OpenLibrary("iffparse.library", 36)))
  132.     {
  133.     struct IFFHandle *iff;
  134.     long error;
  135.     if(iff = AllocIFF())
  136.     {
  137.         if(iff->iff_Stream = (ULONG)OpenClipboard(unit))
  138.         {
  139.         InitIFFasClip(iff);
  140.         if(!(error = OpenIFF(iff, IFFF_READ)))
  141.         {
  142.             if(!(error = StopChunk(iff, ID_FTXT, ID_CHRS)))
  143.             {
  144.             struct ContextNode *cn;
  145.             if(!(error = ParseIFF(iff, IFFPARSE_SCAN)))
  146.             {
  147.                 cn = CurrentChunk(iff);
  148.                 if(cn && (cn->cn_Type == ID_FTXT) && (cn->cn_ID == ID_CHRS))
  149.                 {
  150.                 if(text = make_string(cn->cn_Size + 1))
  151.                 {
  152.                     error = ReadChunkBytes(iff, VSTR(text), cn->cn_Size);
  153.                     if(error > 0)
  154.                     {
  155.                     VSTR(text)[error] = 0;
  156.                     error = 0;
  157.                     }
  158.                 }
  159.                 else
  160.                     error = IFFERR_NOMEM;
  161.                 }
  162.                 else
  163.                 error = IFFERR_NOTIFF;    /* ?? */
  164.             }
  165.             }
  166.             CloseIFF(iff);
  167.         }
  168.         CloseClipboard((struct ClipboardHandle *)iff->iff_Stream);
  169.         }
  170.         else
  171.         error = IFFERR_NOMEM;
  172.         FreeIFF(iff);
  173.     }
  174.     else
  175.         error = IFFERR_NOMEM;
  176.     if(error)
  177.     {
  178.         text = NULL;
  179.         cmd_signal(sym_error, list_2(MKSTR("iffparse"), IFFErrorMsgs[-error - 1]));
  180.     }
  181.     CloseLibrary(IFFParseBase);
  182.     IFFParseBase = NULL;
  183.     }
  184.     else
  185.     cmd_signal(sym_error, LIST_1(MKSTR("Can't open iffparse.library")));
  186.     return(text);
  187. }
  188.